home *** CD-ROM | disk | FTP | other *** search
/ Die Ultimative Software-P…i Collection 1996 & 1997 / Die Ultimative Software-Pakete CD-ROM fur Atari Collection 1996 & 1997.iso / g / gnu_c / pmlsrc23.zoo / pmlsrc / tanh.c < prev   
Encoding:
C/C++ Source or Header  |  1994-03-19  |  4.3 KB  |  230 lines

  1. /************************************************************************
  2.  *                                    *
  3.  *                N O T I C E                *
  4.  *                                    *
  5.  *            Copyright Abandoned, 1987, Fred Fish        *
  6.  *                                    *
  7.  *    This previously copyrighted work has been placed into the    *
  8.  *    public domain by the author (Fred Fish) and may be freely used    *
  9.  *    for any purpose, private or commercial.  I would appreciate    *
  10.  *    it, as a courtesy, if this notice is left in all copies and    *
  11.  *    derivative works.  Thank you, and enjoy...            *
  12.  *                                    *
  13.  *    The author makes no warranty of any kind with respect to this    *
  14.  *    product and explicitly disclaims any implied warranties of    *
  15.  *    merchantability or fitness for any particular purpose.        *
  16.  *                                    *
  17.  ************************************************************************
  18.  */
  19.  
  20.  
  21. /*
  22.  *  FUNCTION
  23.  *
  24.  *    tanh   double precision hyperbolic tangent
  25.  *
  26.  *  KEY WORDS
  27.  *
  28.  *    tanh
  29.  *    machine independent routines
  30.  *    math libraries
  31.  *
  32.  *  DESCRIPTION
  33.  *
  34.  *    Returns double precision hyperbolic tangent of double precision
  35.  *    floating point number.
  36.  *
  37.  *  USAGE
  38.  *
  39.  *    double tanh (x)
  40.  *    double x;
  41.  *
  42.  *  REFERENCES
  43.  *
  44.  *    Fortran IV plus user's guide, Digital Equipment Corp. pp B-5
  45.  *
  46.  *  RESTRICTIONS
  47.  *
  48.  *    For precision information refer to documentation of the
  49.  *    floating point library routines called.
  50.  *    
  51.  *  PROGRAMMER
  52.  *
  53.  *    Fred Fish
  54.  *
  55.  *  INTERNALS
  56.  *
  57.  *    Computes hyperbolic tangent from:
  58.  *
  59.  *        tanh(x) = 1.0 for x > TANH_MAXARG
  60.  *            = -1.0 for x < -TANH_MAXARG
  61.  *            =  sinh(x) / cosh(x) otherwise
  62.  *
  63.  */
  64.  
  65. #if !defined (__M68881__) && !defined (sfp004)
  66.  
  67. #include <stdio.h>
  68. #include <math.h>
  69. #include "pml.h"
  70.  
  71. static char funcname[] = "tanh";
  72.  
  73.     struct exception xcpt;
  74.  
  75. double tanh (x)
  76. double x;
  77. {
  78.     register int positive;
  79.  
  80.     if (x > TANH_MAXARG || x < -TANH_MAXARG) {
  81.     if (x > 0.0) {
  82.         positive = 1;
  83.     } else {
  84.         positive = 0;
  85.     }
  86.     xcpt.type = PLOSS;
  87.     xcpt.name = funcname;
  88.     xcpt.arg1 = x;
  89.     if (!matherr (&xcpt)) {
  90.         fprintf (stderr, "%s: PLOSS error\n", funcname);
  91.         errno = ERANGE;
  92.         if (positive) {
  93.         xcpt.retval = 1.0;
  94.         } else {
  95.         xcpt.retval = -1.0;
  96.         }
  97.     }
  98.     } else {
  99.     xcpt.retval = sinh (x) / cosh (x);
  100.     }
  101.     return (xcpt.retval);
  102. }
  103. #endif    /* __M68881__, sfp004    */
  104. #ifdef    sfp004
  105.  
  106. __asm("
  107.  
  108. comm =     -6
  109. resp =    -16
  110. zahl =      0
  111.  
  112. ");    /* end asm    */
  113.  
  114. #endif    sfp004
  115. #if defined (__M68881__) || defined (sfp004)
  116.  
  117.    __asm(".text; .even");
  118.  
  119. # ifdef    ERROR_CHECK
  120.  
  121.     __asm("
  122.  
  123. _Overflow:
  124.     .ascii \"OVERFLOW\\0\"
  125. _Domain:
  126.     .ascii \"DOMAIN\\0\"
  127. _Error_String:
  128.     .ascii \"tanh: %s error\\n\\0\"
  129. .even
  130. | pml compatible tanhgent
  131. | m.ritzert 7.12.1991
  132. | ritzert@dfg.dbp.de
  133. |
  134. |    /* NAN  = {7fffffff,ffffffff}        */
  135. |    /* +Inf = {7ff00000,00000000}        */
  136. |    /* -Inf = {fff00000,00000000}        */
  137. |    /* MAX_D= {7fee42d1,30773b76}        */
  138. |    /* MIN_D= {ffee42d1,30773b76}        */
  139.  
  140. .even
  141. double_max:
  142.     .long    0x7fee42d1
  143.     .long    0x30273b76
  144. double_min:
  145.     .long    0xffee42d1
  146.     .long    0x30273b76
  147. NaN:
  148.     .long    0x7fffffff
  149.     .long    0xffffffff
  150. p_Inf:
  151.     .long    0x7ff00000
  152.     .long    0x00000000
  153. m_Inf:
  154.     .long    0xfff00000
  155.     .long    0x00000000
  156. ");
  157. #endif    ERROR_CHECK
  158.  
  159.     __asm(".even
  160. .globl _tanh
  161. _tanh:
  162.     ");    /* end asm    */
  163.  
  164. #endif    /* __M68881__ || sfp004    */
  165. #ifdef    __M68881__
  166.  
  167.     __asm("
  168.     ftanhd    a7@(4), fp0    | tanh
  169.     fmoved    fp0,a7@-    | push result
  170.     moveml    a7@+,d0-d1    | return_value
  171.     ");    /* end asm    */
  172.  
  173. #endif    __M68881__
  174. #ifdef    sfp004
  175.     __asm("
  176.     lea    0xfffa50,a0
  177.     movew    #0x5409,a0@(comm)    | specify function
  178.     cmpiw    #0x8900,a0@(resp)    | check
  179.     movel    a7@(4),a0@        | load arg_hi
  180.     movel    a7@(8),a0@        | load arg_low
  181.     movew    #0x7400,a0@(comm)    | result to d0
  182.     .long    0x0c688900, 0xfff067f8    | wait
  183.     movel    a0@,d0
  184.     movel    a0@,d1
  185.     ");    /* end asm    */
  186.  
  187. #endif    sfp004
  188. #if defined (__M68881__) || defined (sfp004)
  189. # ifdef    ERROR_CHECK
  190.     __asm("
  191.     lea    double_max,a0    |
  192.     swap    d0        | exponent into lower word
  193.     cmpw    a0@(16),d0    | == NaN ?
  194.     beq    error_nan    |
  195.     swap    d0        | result ok,
  196.     rts            | restore d0
  197. ");
  198. #ifndef    __MSHORT__
  199. __asm("
  200. error_nan:
  201.     moveml    a0@(24),d0-d1    | result = +inf
  202.     moveml    d0-d1,a7@-
  203.     movel    #62,_errno    | NAN => errno = EDOM
  204.     pea    _Domain        | for printf
  205. ");
  206. #else    __MSHORT__
  207. __asm("
  208. error_nan:
  209.     moveml    a0@(24),d0-d1    | result = +inf
  210.     moveml    d0-d1,a7@-
  211.     movew    #62,_errno    | NAN => errno = EDOM
  212.     pea    _Domain        | for printf
  213. ");
  214. #endif    __MSHORT__
  215. __asm("
  216. error_exit:
  217.     pea    _Error_String    |
  218.     pea    __iob+52    |
  219.     jbsr    _fprintf    |
  220.     addl    #12,a7        |
  221.     moveml    a7@+,d0-d1
  222.     rts
  223.     ");
  224. # else    ERROR_CHECK
  225.  
  226. __asm("rts");
  227.  
  228. # endif    ERROR_CHECK
  229. #endif /* __M68881__ || sfp004    */
  230.